//
//  clubViewController.m
//  ridingOnFoot
//
//  Created by 刘京涛 on 16/3/11.
//  Copyright © 2016年 刘京涛. All rights reserved.
//

#import "clubViewController.h"
#import "RankViewController.h"
#import "CreatViewController.h"
#import "tool.h"
#import "ClubTableViewCell.h"
#import "RequestUrl.h"
#import "ClubModel.h"
#import <UIImageView+WebCache.h>

@interface clubViewController ()<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UIScrollView * scrollView;
@property(nonatomic,strong) UISearchBar * search;
@property(nonatomic,strong) UIView * titleView;
@property(nonatomic,strong) UITableView * clubListTableView;
@property(nonatomic,strong) UIButton * nearbyButton;

@property(nonatomic,strong) NSMutableArray * arrAllData;

@end

@implementation clubViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     self.view.backgroundColor=[UIColor whiteColor];

    UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(kscreenWidth/2 - 80, 0, 160, 44)];
    titleLabel.text = @"俱乐部";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.textColor = [UIColor whiteColor];
    [self.navigationController.navigationBar addSubview:titleLabel];
    
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:50/255.0 green:50/255.0 blue:50/255.0 alpha:1];
    
    [self setView];
    
     self.search.delegate = self;
}


-(void)setView{
    
    self.search = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, kscreenWidth, 40)];
    self.search.placeholder = @"编号,名称和附近车队搜索";
    [self.view addSubview:self.search];
    
    //左
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"rank_icon@2x.png"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftAction)];
    self.navigationController.navigationBar.translucent=NO;
    //右
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"xingzhe_nav_more-03@2x.png"] style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction)];
    
    self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, self.search.frame.size.height + 20, kscreenWidth, kscreenHeight)];
//    self.scrollView.backgroundColor = [UIColor orangeColor];
    self.scrollView.contentSize = CGSizeMake(kscreenWidth, kscreenHeight * 2);
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    
    
}

- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    
    if (!self.navigationController.navigationBarHidden) {
        
        self.navigationController.navigationBarHidden = YES;
        //键盘
        self.search.returnKeyType = UIReturnKeySearch;
        [self.search resignFirstResponder];
        [self.search setKeyboardType:(UIKeyboardTypeWebSearch)];
        
        self.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kscreenWidth, 20)];
        self.titleView.backgroundColor = [UIColor grayColor];
        self.titleView.alpha = 0.5;
        [self.view addSubview:self.titleView];
        
        self.search.frame = CGRectMake(0, 20, kscreenWidth, 40);
        self.search.backgroundColor = [UIColor grayColor];
        self.search.showsCancelButton = YES;
        
        
        
        //附近按钮
        self.nearbyButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 50, 110, 30)];
        self.nearbyButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 50, 110, 30)];
        self.nearbyButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 30, 110, 30)];
        [self.nearbyButton setTitle:@"附近俱乐部" forState:(UIControlStateNormal)];
//        self.nearbyButton.font = [UIFont systemFontOfSize:17];
        [self.nearbyButton setTitleColor:[UIColor blueColor] forState:(UIControlStateNormal)];
        [self.nearbyButton.layer setCornerRadius:15.0];
        [self.nearbyButton.layer setBorderWidth:1];
        [self.nearbyButton.layer setBorderColor:[UIColor blueColor].CGColor];
        [self.nearbyButton addTarget:self action:@selector(nearbyButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
        [self.scrollView addSubview:self.nearbyButton];

        
    }
    
    
    return YES;
}


-(void)nearbyButtonAction{
    
    self.clubListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,self.search.frame.size.height + 20, kscreenWidth, kscreenHeight - 40) style:(UITableViewStylePlain)];
    self.clubListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,self.search.frame.size.height + 20, kscreenWidth, kscreenHeight - 40) style:(UITableViewStylePlain)];
    self.listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,self.search.frame.size.height + 20, kscreenWidth, kscreenHeight - 40) style:(UITableViewStylePlain)];
    self.clubListTableView = [[UITableView alloc]initWithFrame:CGRectMake(0,self.search.frame.size.height + 20, kscreenWidth, kscreenHeight - 108) style:(UITableViewStylePlain)];
//    self.listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.clubListTableView.rowHeight = 84;
        [self.view addSubview:self.clubListTableView];
    self.clubListTableView.delegate = self;
    self.clubListTableView.dataSource =self;
    [self.clubListTableView registerNib:[UINib nibWithNibName:@"ClubTableViewCell" bundle:nil] forCellReuseIdentifier:@"ClubTableViewCell"];
    
    [self setData];
    [self.clubListTableView reloadData];
    
}


-(void)setData{
    //keyword=&latitude=40.029195&longitude=116.337080&page=0
    
    NSDictionary * nearbyDic = @{@"latitude":@40.029195,@"longitude":@116.337080,@"page":@0};
    
    [RequestUrl requestWith:GET URL:URLNEARBY condition:nearbyDic SuccessBlock:^(id item) {
        
        self.arrAllData = [NSMutableArray array];
        for (NSDictionary * dic in item) {
            
            ClubModel * model = [[ClubModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.arrAllData addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
           
            [self.clubListTableView reloadData];
        });
        
        
    } failBlock:^(NSError *err) {
        
    }];
    
}






-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.arrAllData.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ClubTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ClubTableViewCell" forIndexPath:indexPath];
    
    ClubModel * model = self.arrAllData[indexPath.row];
    CGRect rect = cell.teamNumber.frame;
    rect.size.width = 50;
    cell.teamNumber.frame = rect;
    cell.teamNumber.backgroundColor = [UIColor clearColor];
    cell.teamNumber.text = [NSString stringWithFormat:@"#%ld",model.teamId];
    
    cell.teamTitle.text = model.teamTitle;
    
    if (model.teamCityName.length == 0) {
        cell.teamCitynameImageView.hidden = YES;
    }
    cell.teamCityName.text = model.teamCityName;
    cell.teamMiles.text = [NSString stringWithFormat:@"%ld",model.teamMiles];
    cell.teamUserCounts.text = [NSString stringWithFormat:@"%ld",model.teamUserCounts];
    cell.teamAvatar.layer.masksToBounds = YES;
    if (model.teamAvatar.length != 0) {
        [cell.teamAvatar sd_setImageWithURL:[NSURL URLWithString:model.teamAvatar]];
    }else{
        cell.teamAvatar.image = [UIImage imageNamed:@"chuyin.png"];
    }

    
    return cell;
    
}


//取消
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
    self.navigationController.navigationBarHidden = NO;
    
    [self.titleView removeFromSuperview];
    
    if ([self.view.subviews containsObject:self.clubListTableView]) {
        
        [self.clubListTableView removeFromSuperview];
    }

    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
//    self.tableView.allowsSelection=YES;
//    self.tableView.scrollEnabled=YES;
    
    self.search.frame = CGRectMake(0, 0, kscreenWidth, 40);
    self.search.backgroundColor = [UIColor grayColor];
    
        
    [self.nearbyButton removeFromSuperview];
    
}


-(void)rightAction{
    
    CreatViewController * creatVC = [[CreatViewController alloc]init];
    
    [self.navigationController pushViewController:creatVC animated:YES];
    
}

-(void)leftAction{
    
    RankViewController * rankVC = [[RankViewController alloc]init];
    
    [self.navigationController pushViewController:rankVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
